"""基金净值与规模查询模块"""

JZ_SQL = """
SELECT * FROM (
    SELECT 
        t.TradingDay AS 交易日,
        t.UnitNV AS 单位净值,
        t.UnitNVRestored AS 复权单位净值,
        CONCAT(ROUND(t.NVDailyGrowthRate * 100, 2), '%') AS 单位净值日增长率,
        CONCAT(ROUND(t.NVRDailyGrowthRate * 100, 2), '%') AS 复权单位净值日增长率,
        s.ChiName AS 基金全称,
        s.ChiNameAbbr AS 基金简称,
        s.SecuCode AS 基金代码
    FROM MF_FundNetValueRe t
    INNER JOIN SecuMain s ON t.InnerCode = s.InnerCode
    WHERE s.ListedState = 1  -- 上市状态
      AND s.SecuCategory = 8  -- 基金类别
      AND (
          (:fund_code IS NOT NULL AND s.SecuCode = :fund_code)
          OR 
          (:fund_name IS NOT NULL AND s.ChiName LIKE '%' || :fund_name || '%')
          OR
          (:fund_short_name IS NOT NULL AND s.ChiNameAbbr LIKE '%' || :fund_short_name || '%')
      )
      AND (:start_date IS NULL OR t.TradingDay >= TO_DATE(:start_date, 'YYYY-MM-DD'))  -- 开始日期筛选条件
      AND (:end_date IS NULL OR t.TradingDay <= TO_DATE(:end_date, 'YYYY-MM-DD'))  -- 结束日期筛选条件
    ORDER BY t.TradingDay DESC
) WHERE ROWNUM <= 3600
"""

# 查询指定基金在过去25个月内的基金规模变化情况，返回最近的最多30条记录
GM_SQL = """
SELECT * FROM (
SELECT 
    mfMfi.ENDDATE AS 报告日期,
    mfMfi.NETASSETSVALUE AS 基金规模_元
FROM 
    MF_MainFinancialIndexQ mfMfi
INNER JOIN MF_FundArchives naf ON naf.innercode = mfMfi.innercode
WHERE mfMfi.ENDDATE >= (SELECT ADD_MONTHS(SYSDATE, - 25) FROM DUAL)
AND mfMfi.innercode = :innercode
AND mfMfi.NETASSETSVALUE IS NOT NULL
ORDER BY 
    mfMfi.ENDDATE DESC)
WHERE ROWNUM <= 30
"""

# 净值和规模查询函数
def get_net_value(fund_code, fund_name, fund_short_name, start_date, end_date):
    """获取基金净值相关信息（包含单位净值和复权单位净值）"""
    return JZ_SQL, {'fund_code': fund_code, 'fund_name': fund_name, 'fund_short_name': fund_short_name, 'start_date': start_date, 'end_date': end_date}

def get_fund_scale(innercode):
    """获取基金规模变化"""
    return GM_SQL, {'innercode': innercode}